package com.liveneo.adapter.mobile.service.bussniess.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.liveneo.adapter.base.entity.PrpmCaseInfo;
import com.liveneo.adapter.base.entity.PrpmImageSurvey;
import com.liveneo.adapter.base.entity.PrpmLossItem;
import com.liveneo.adapter.base.entity.PrpmLossItemHistory;
import com.liveneo.adapter.base.entity.PrpmOrg;
import com.liveneo.adapter.base.entity.PrpmSurveyUser;
import com.liveneo.adapter.base.support.LogUtils;
import com.liveneo.adapter.mobile.model.BaseResponse;
import com.liveneo.adapter.mobile.model.dto.CaseInfoDto;
import com.liveneo.adapter.mobile.model.dto.LossItemDto;
import com.liveneo.adapter.mobile.model.request.CaseInfoSelfRequest;
import com.liveneo.adapter.mobile.model.request.LossItemListRequest;
import com.liveneo.adapter.mobile.model.request.SurveyFinishRequest;
import com.liveneo.adapter.mobile.model.request.TurnPersonSurveyRequest;
import com.liveneo.adapter.mobile.model.request.VersionRequest;
import com.liveneo.adapter.mobile.model.response.CaseInfoSelfResponse;
import com.liveneo.adapter.mobile.model.response.LossItemListResponse;
import com.liveneo.adapter.mobile.model.response.VersionResponse;
import com.liveneo.adapter.mobile.service.CaseInfoService;
import com.liveneo.adapter.mobile.service.LossItemService;
import com.liveneo.adapter.mobile.service.MobileImageService;
import com.liveneo.adapter.mobile.service.MobileLossService;
import com.liveneo.adapter.mobile.service.PrpmLossItemHistoryService;
import com.liveneo.adapter.mobile.service.PrpmOrgService;
import com.liveneo.adapter.mobile.service.PrpmSurveyUserService;
import com.liveneo.adapter.mobile.service.PrpmUploadVideoService;
import com.liveneo.adapter.mobile.service.bussniess.MobileBussService;
import com.liveneo.adapter.mobile.support.MobileReturnCodeSupport;
import com.liveneo.adapter.mobile.util.Constant;
import com.liveneo.adapter.mobile.util.DateUtil;
import com.liveneo.adapter.mobile.util.OptConstant;
import com.liveneo.adapter.mobile.util.UUIDUtils;
import com.liveneo.adapter.outinterface.util.ResultInfo;
import com.liveneo.adapter.outinterface.web.platform.ClaimInterface;
import com.liveneo.adapter.outinterface.web.platform.request.TaskStatusRequest;
import com.liveneo.adapter.webservice.client.claim.PolicyInfo;

@Service
public class MobileBussServiceImpl implements MobileBussService {

    @Autowired
    private LossItemService lossItemService;

    @Autowired
    private CaseInfoService caseInfoService;

    @Autowired
    private PrpmLossItemHistoryService prpmLossItemHistoryService;

    @Autowired
    private PrpmOrgService prpmOrgService;
    @Autowired
    private PrpmUploadVideoService prpmUploadVideoService;

    @Autowired
    private MobileLossService mobileLossService;
    @Autowired
    private MobileImageService mobileImageService;

    @Autowired
    private PrpmSurveyUserService prpmSurveyUserService;
    
    private static String reassignReType = "10";
    private static String reassignMsg = "转人工";

    /**
     * C端转人工 1调用SLD07接口 2更新任务状态(更新状态为申请改派) 3更新整案状态 4记录状态变更历史表
     * 
     * @throws Exception
     */
    @Override
    public BaseResponse requestTurnPeronSurvey(TurnPersonSurveyRequest request) throws Exception {
        String uuid = request.getCaseUuid();
        String userCode = request.getUserId();
        String userType = "B";
        List<LossItemDto> requestList = request.getLossItemList();
        BaseResponse response = new BaseResponse();
        PrpmCaseInfo prpmCaseInfo = caseInfoService.selectByPrimaryKey(uuid);// 根据uuid查询案件信息
        PrpmSurveyUser surveyUser = null;
        if (StringUtils.isNotBlank(request.getProjectCode()))
            userType = request.getProjectCode();
        if ("A".equals(userType)) {
            surveyUser = prpmSurveyUserService.findUserByUserCodeAndType(request.getUserId(), "2");
        }
        if ("B".equals(userType)) {
            surveyUser = prpmSurveyUserService.findUserByUserCodeAndType(request.getUserId(), "1");
        }
        if (prpmCaseInfo == null || "".equals(prpmCaseInfo)) {
            response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_FAIL,
                    MobileReturnCodeSupport.SYS_CODE, MobileReturnCodeSupport.KEY_MOBILE_00016, BaseResponse.class);
        }
        List<String> lossUuidList = new ArrayList<String>();
        List<PrpmLossItemHistory> lossItemHistoryList = new ArrayList<PrpmLossItemHistory>();
        for(LossItemDto lossItemDto : requestList){
            String lossId = lossItemDto.getLossId();
            String lossUuid = lossItemDto.getLossUuid();
            PrpmLossItem LossItemDb = lossItemService.selectByPrimaryKey(lossUuid);
            if(LossItemDb != null && lossId.equals(LossItemDb.getLossId())){
                lossUuidList.add(lossUuid);
                PrpmLossItemHistory prpmLossItemHistory = new PrpmLossItemHistory();
                prpmLossItemHistory.setCaseInfoId(LossItemDb.getCaseInfoId());
                prpmLossItemHistory.setCaseNo(LossItemDb.getCaseNo());
                prpmLossItemHistory.setLossId(LossItemDb.getLossId());
                prpmLossItemHistory.setLossItemId(LossItemDb.getLossItemId());
                prpmLossItemHistory.setLossType(LossItemDb.getLossType());
                prpmLossItemHistory.setReassignPlat(Constant.REASSIGN_PLAT_APP);
                prpmLossItemHistory.setOptType(OptConstant.MP_C_CHANGE_B);
                prpmLossItemHistory.setOperateTime(new Date());
                prpmLossItemHistory.setUserCode(userCode);
                prpmLossItemHistory.setCreateBy(surveyUser != null ? surveyUser.getSurveyName() : request.getUserId());
                prpmLossItemHistory.setCreateTime(new Date());
                lossItemHistoryList.add(prpmLossItemHistory);
            }
        }
        
        // 1调用SLD07接口
        TaskStatusRequest claimRequest = new TaskStatusRequest();
        claimRequest.setLogUuid(request.getLogUuid());
        claimRequest.setCaseNo(prpmCaseInfo.getCaseNo());
        claimRequest.setCaseUuid(request.getCaseUuid());
        claimRequest.setLossUuidList(lossUuidList);
        claimRequest.setTaskStatus(Constant.CLAIM_CLIENTS_APPLYTURNPERSON);
        claimRequest.setReassignReType(reassignReType);
        claimRequest.setReassignMsg(reassignMsg);
        ResultInfo resultInfo = ClaimInterface.updateTaskStatus(claimRequest);
        if (resultInfo.getResultCode().equals("000")) {
            // 2更新任务状态
            PrpmLossItem prpmLossItem = new PrpmLossItem();
            prpmLossItem.setLossItemIdList(lossUuidList);
            prpmLossItem.setLossStatus(Constant.CASE_APPLYSCHE);
            prpmLossItem.setModifyBy(userCode);
            prpmLossItem.setModifyTime(new Date());
            lossItemService.updateLossStatusByUuidList(prpmLossItem);
            // 3更新整案状态
            PrpmCaseInfo record = new PrpmCaseInfo();
            record.setCaseInfoId(uuid);
            record.setModifyBy(surveyUser != null ? surveyUser.getSurveyName() : request.getUserId());
            record.setModifyTime(new Date());
            record.setCaseStatus(Constant.CASE_STATUS_DEALING);
            caseInfoService.updateByPrimaryKeySelective(record);
            // 4记录状态变更历史表
            prpmLossItemHistoryService.saveList(lossItemHistoryList);
            response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_SUCCESS,
                    MobileReturnCodeSupport.SYS_CODE, MobileReturnCodeSupport.CODE_SUCCESS, BaseResponse.class);
        } else {
            response = MobileReturnCodeSupport.buildReponse2(MobileReturnCodeSupport.CODE_FAIL,
                    MobileReturnCodeSupport.SYS_CODE, resultInfo.getResultMsg(), BaseResponse.class);
        }
        
//      List<PrpmLossItem> lossItemList = lossItemService.getLossItemByCaseId(uuid, Constant.MOBILEUSER_CUSTOMER, userCode);
//      if (lossItemList != null && lossItemList.size() > 0) {
//          for (PrpmLossItem prpmLossItem : lossItemList) {
//              lossUuidList.add(prpmLossItem.getLossItemId());
//              PrpmLossItemHistory prpmLossItemHistory = new PrpmLossItemHistory();
//              prpmLossItemHistory.setCaseInfoId(prpmLossItem.getCaseInfoId());
//              prpmLossItemHistory.setCaseNo(prpmLossItem.getCaseNo());
//              prpmLossItemHistory.setLossId(prpmLossItem.getLossId());
//              prpmLossItemHistory.setLossItemId(prpmLossItem.getLossItemId());
//              prpmLossItemHistory.setLossType(prpmLossItem.getLossType());
//              prpmLossItemHistory.setReassignPlat(Constant.REASSIGN_PLAT_APP);
//              prpmLossItemHistory.setOptType(OptConstant.MP_C_CHANGE_B);
//              prpmLossItemHistory.setOperateTime(new Date());
//              prpmLossItemHistory.setUserCode(userCode);
//              prpmLossItemHistory.setCreateBy(surveyUser != null ? surveyUser.getSurveyName() : request.getUserId());
//              prpmLossItemHistory.setCreateTime(new Date());
//              lossItemHistoryList.add(prpmLossItemHistory);
//          }
//      } else {
//          response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_FAIL,
//                  MobileReturnCodeSupport.SYS_CODE, MobileReturnCodeSupport.KEY_MOBILE_00051, BaseResponse.class);
//      }

        return response;
    }

    /**
     * 自助：获取报案信息 1查询案件信息 2查询该用户的任务信息 3将任务信息中案件状态和定损单状态汇总
     * 
     */
    @Override
    public CaseInfoSelfResponse getRegistInfo(CaseInfoSelfRequest request) {
        String phoneNo = request.getPhoneNo();
        CaseInfoSelfResponse response = new CaseInfoSelfResponse();
        String agtID = ""; // 最近进行视频连线的坐席-中兴工号
        try {
            List<PrpmLossItem> prpmLossItemList = lossItemService.getLossItemByPhoneNo(phoneNo);// 根据手机号查prpmlossitem
            List<String> caseUUidList = new ArrayList<String>();
            Map<String, Integer> caseStatusMap = new HashMap<String, Integer>();
            // 0未生成定损单1已生成定损单
            Map<String, Integer> lossStatusMap = new HashMap<String, Integer>();
            // 取出caseUUid放入list,此时caseUUid为重复的
            if (prpmLossItemList != null && prpmLossItemList.size() > 0) {
                for (int i = 0; i < prpmLossItemList.size(); i++) {
                    String caseUUid = prpmLossItemList.get(i).getCaseInfoId();
                    if (!caseStatusMap.containsKey(prpmLossItemList.get(i).getCaseInfoId())) {
                        caseStatusMap.put(prpmLossItemList.get(i).getCaseInfoId(),
                                prpmLossItemList.get(i).getLossStatus());
                        caseUUidList.add(caseUUid);
                    }
                }
                for (int i = 0; i < caseUUidList.size(); i++) {
                    List<PrpmLossItem> prpmLossStatusList = new ArrayList<PrpmLossItem>();
                    for (int j = 0; j < prpmLossItemList.size(); j++) {//获取单个case下的所有lossitem
                        if (StringUtils.endsWith(prpmLossItemList.get(j).getCaseInfoId(), caseUUidList.get(i))) {
                            prpmLossStatusList.add(prpmLossItemList.get(j));
                        }
                    }
                    int confirmCount = 0;
                    int unConfirmCount = 0;
                    int unDealCount = 0;
                    int finishedCount = 0;
                    for (int j = 0; j < prpmLossStatusList.size(); j++) {
                        if (prpmLossStatusList.get(j).getLossitemStatus() != 0
                                && prpmLossStatusList.get(j).getLossitemStatus() != 1) {
                            confirmCount++;
                        } else {
                            unConfirmCount++;
                        }
                        if(prpmLossStatusList.get(j).getLossStatus()==0){
                            unDealCount++;
                        }else if(prpmLossStatusList.get(j).getLossStatus()==2||
                                prpmLossStatusList.get(j).getLossStatus()==4||
                                prpmLossStatusList.get(j).getLossStatus()==5||
                                prpmLossStatusList.get(j).getLossStatus()==6){
                            finishedCount++;
                        }
                    }
                    if (confirmCount == prpmLossStatusList.size()) {
                        lossStatusMap.put(caseUUidList.get(i), 3);//1未确认2部分确认3全部确认
                    }else if(unConfirmCount == prpmLossStatusList.size()) {
                        lossStatusMap.put(caseUUidList.get(i), 1);
                    }else{
                        lossStatusMap.put(caseUUidList.get(i), 2);
                    }
                    //0-待处理 1-处理中 2，处理完成
                    caseStatusMap.remove(caseUUidList.get(i));
                    if (unDealCount == prpmLossStatusList.size()) {
                        caseStatusMap.put(caseUUidList.get(i),0);
                    }else if(finishedCount == prpmLossStatusList.size()) {
                        caseStatusMap.put(caseUUidList.get(i), 2);
                    }else{
                        caseStatusMap.put(caseUUidList.get(i), 1);
                    }
                }
                if (caseUUidList != null && caseUUidList.size() > 0) {
                    List<PrpmCaseInfo> caseList = caseInfoService.getCaseInfo(caseUUidList);// 获取caseUUid获取prpmcaseinfo
                    List<CaseInfoDto> caseInfoList = new ArrayList<CaseInfoDto>();
                    if (caseList != null && caseList.size() > 0) {
                        for (int i = 0; i < caseList.size(); i++) {
                            PrpmOrg prpmOrg = prpmOrgService.selectByPrimaryKey(caseList.get(i).getOrgId());
                            CaseInfoDto caseInfoDto = new CaseInfoDto();
                            BeanUtils.copyProperties(caseInfoDto, caseList.get(i));
                            caseInfoDto.setCaseUuid(caseList.get(i).getCaseInfoId());
                            caseInfoDto.setUnitCode(caseList.get(i).getOrgCode());
                            caseInfoDto.setFinishCaseTimeTemp(caseList.get(i).getCreateTime());
                            caseInfoDto.setSurveyPalce(caseList.get(i).getSurveyPlace());
                            caseInfoDto.setSurveyDate(DateUtil.dateToStrs(caseList.get(i).getSurveyDate()));
                            caseInfoDto.setDamageDate(DateUtil.dateToStrs(caseList.get(i).getDamageDate()));
                            caseInfoDto.setMobileUser(3);
                            // 不考虑个人名下任务状态不一致的情况，取第一个任务的状态作为C端案件状态
                            caseInfoDto.setCaseStatus(caseStatusMap.get(caseList.get(i).getCaseInfoId()));
                            caseInfoDto.setLossStatus(lossStatusMap.get(caseList.get(i).getCaseInfoId()));
                            if (prpmOrg != null && !"".equals(prpmOrg)) {
                                caseInfoDto.setVcid(prpmOrg.getOrgVcid());
                                caseInfoDto.setSkillID(prpmOrg.getOrgSkill());
                            }
                            agtID = prpmUploadVideoService.getRecentAgtID(caseList.get(i).getCaseInfoId());
                            // 查询核心P12接口获取案件信息
                            ResultInfo resultInfo = ClaimInterface.findCaseInfo(caseList.get(i).getCaseNo());
                            if (resultInfo.getCaseInfo() != null) {
                                List<PolicyInfo> policyInfoList = resultInfo.getCaseInfo().getPolicyLst();
                                // 添加保单信息 
                                for (PolicyInfo policyInfo : policyInfoList) {
                                    if (policyInfo.getSpeciesName().contains("商业险")) {
                                        caseInfoDto.setPolicyNumber(policyInfo.getPolicyNo());
                                    } 
                                    // 
                                    if(policyInfoList.size() == 1){
                                        caseInfoDto.setPolicyNumber(policyInfo.getPolicyNo());
                                    }
                                }
                            }
                            caseInfoDto.setAgtID(agtID);
                            List<LossItemDto> lossItemList = new ArrayList<LossItemDto>();
                            for (PrpmLossItem lossitem : prpmLossItemList) {
                                if (StringUtils.equals(caseList.get(i).getCaseInfoId(), lossitem.getCaseInfoId())) {
                                    LossItemDto dto = new LossItemDto();
                                    dto.setLossId(lossitem.getLossId());
                                    dto.setLossUuid(lossitem.getLossItemId());
                                    dto.setLossUStatus(String.valueOf(lossitem.getLossStatus()));
                                    lossItemList.add(dto);
                                }
                            }
                            Collections.sort(lossItemList);
                            caseInfoDto.setLossItemList(lossItemList);

                            caseInfoList.add(caseInfoDto);
                        }
                    }
                    response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_SUCCESS,
                            MobileReturnCodeSupport.SYS_CODE, null, CaseInfoSelfResponse.class);
                    response.setCaseInfoList(caseInfoList);
                }
            } else {
                response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_SUCCESS,
                        MobileReturnCodeSupport.SYS_CODE, null, CaseInfoSelfResponse.class);
            }
        } catch (Exception e) {
            LogUtils.error("MP11->logUuid:" + request.getLogUuid() + "获取案件列表失败，", e);
            response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_INTERNAL_ERROR,
                    MobileReturnCodeSupport.SYS_CODE, e.getMessage(), CaseInfoSelfResponse.class);
        }
        return response;
    }

    @Override
    public VersionResponse getNewVersion(VersionRequest request) throws Exception {
        return null;
    }

    @Override
    public BaseResponse surveyFinish(SurveyFinishRequest sr) throws Exception {
        BaseResponse response = null;
        String userType = "";
        PrpmSurveyUser surveyUser = null;
        if (StringUtils.isNotBlank(sr.getProjectCode()))
            userType = sr.getProjectCode();
        if ("A".equals(userType)) {
            surveyUser = prpmSurveyUserService.findUserByUserCodeAndType(sr.getUserId(), "2");
        }
        if ("B".equals(userType)) {
            surveyUser = prpmSurveyUserService.findUserByUserCodeAndType(sr.getUserId(), "1");
        }
        // 查询定损任务信息
        PrpmImageSurvey ps = new PrpmImageSurvey();
        // 如果有分号分隔的话，就是多个任务一起提交
        // 如果有多个任务，判断有无查勘任务，如果有查勘任务就过滤掉查勘任务，但是需要绑定到第一个损失项下面
        List<PrpmLossItemHistory> lossItemHistoryList = new ArrayList<PrpmLossItemHistory>();
        List<String> lossUuidAList = new ArrayList<String>();
        if (sr.getLossUuid().indexOf(",") > 0) {
            String[] lossUuidList = StringUtils.split(sr.getLossUuid(), ",");
            String[] lossIdList = StringUtils.split(sr.getLossId(), ",");
            if (StringUtils.endsWith(lossIdList[0], "0")) {
                lossUuidAList.add(lossUuidList[0]);
                for (int i = 1; i < lossUuidList.length; i++) {
                    PrpmLossItem prpmLossItem = mobileLossService.getLossItemByLossUuid(lossUuidList[i]);
                    if (prpmLossItem != null) {
                        ps.setCaseNo(prpmLossItem.getCaseNo());
                        ps.setLossId(prpmLossItem.getLossId());
                        ps.setCaseInfoId(prpmLossItem.getCaseInfoId());
                        ps.setLossItemId(prpmLossItem.getLossItemId());
                        ps.setLossType(String.valueOf(prpmLossItem.getLossType()));
                        ps.setImageSurveyId(UUIDUtils.getUUIDStr());
                        ps.setSurveyUserCode(sr.getUserId());
                        ps.setDealFlag("0");
                        ps.setRemark(sr.getRemark());
                        String mobileUser = "";
                        int userRType = 2;
                        if (StringUtils.isNotBlank(sr.getProjectCode()))
                            mobileUser = sr.getProjectCode();
                        if ("A".equals(mobileUser)) {
                            userRType = 1;
                            ps.setSurveyUserUuid(surveyUser.getSurveyUserId());
                        }
                        if ("B".equals(mobileUser)) {
                            userRType = 2;
                            ps.setSurveyUserUuid(surveyUser.getSurveyUserId());
                        }
                        if ("C".equals(mobileUser)) {
                            userRType = 3;
                            ps.setSurveyUserUuid(sr.getUserId());
                        }
                        ps.setUserType(userRType);
                        PrpmImageSurvey prpmImageSurvey = mobileImageService.getImageSurveyByLossUuid(lossUuidList[i]);
                        if (i == 1) {
                            ps.setRelationId(lossUuidList[0]);
                        }
//                      ps.setUserCode(sr.getUserId());
//                      ps.setUserName(surveyUser != null ? surveyUser.getSurveyName() : sr.getUserId());
                        ps.setUserId(surveyUser != null ? surveyUser.getSurveyName() : sr.getUserId());
                        
                        if (prpmImageSurvey != null) {
                            ps.setCreateTime(new Date());
                            ps.setModifyBy(surveyUser != null ? surveyUser.getSurveyName() : sr.getUserId());
                            ps.setModifyTime(new Date());
                            mobileImageService.updateImageSurveyByLossUuid(ps);
                        } else {
                            ps.setIsDealing("1");// 未认领
                            ps.setCreateTime(new Date());
                            ps.setCreateBy(surveyUser != null ? surveyUser.getSurveyName() : sr.getUserId());
                            mobileImageService.saveSurveyImage(ps);
                        }
                    }
                    PrpmLossItemHistory prpmLossItemHistory = new PrpmLossItemHistory();
                    prpmLossItemHistory.setCaseInfoId(prpmLossItem.getCaseInfoId());
                    prpmLossItemHistory.setCaseNo(prpmLossItem.getCaseNo());
                    prpmLossItemHistory.setLossId(prpmLossItem.getLossId());
                    prpmLossItemHistory.setLossItemId(prpmLossItem.getLossItemId());
                    prpmLossItemHistory.setLossType(prpmLossItem.getLossType());
                    prpmLossItemHistory.setReassignPlat(Constant.REASSIGN_PLAT_APP);
                    prpmLossItemHistory.setOptType(OptConstant.MP_UPLOAD_SUCC);
                    prpmLossItemHistory.setOperateTime(new Date());
                    prpmLossItemHistory.setUserCode(sr.getUserId());
                    prpmLossItemHistory.setCreateBy(surveyUser != null ? surveyUser.getSurveyName() : sr.getUserId());
                    prpmLossItemHistory.setCreateTime(new Date());
                    lossItemHistoryList.add(prpmLossItemHistory);
                    lossUuidAList.add(lossUuidList[i]);
                }
            } else {
                for (int i = 0; i < lossUuidList.length; i++) {
                    PrpmLossItem prpmLossItem = mobileLossService.getLossItemByLossUuid(lossUuidList[i]);
                    if (prpmLossItem != null) {
                        ps.setCaseNo(prpmLossItem.getCaseNo());
                        ps.setLossItemId(lossUuidList[i]);
                        ps.setSurveyUserUuid(sr.getUserId());
                        ps.setImageSurveyId(UUIDUtils.getUUIDStr());
                        ps.setSurveyUserCode(sr.getUserId());
                        ps.setLossId(lossIdList[i]);
                        ps.setCreateTime(new Date());
                        ps.setDealFlag("0");
                        ps.setRemark(sr.getRemark());
//                      ps.setUserCode(sr.getUserId());
//                      ps.setUserName(surveyUser != null ? surveyUser.getSurveyName() : sr.getUserId());
                        ps.setUserId(surveyUser != null ? surveyUser.getSurveyName() : sr.getUserId());
                        String mobileUser = "";
                        int userRType = 2;
                        if (StringUtils.isNotBlank(sr.getProjectCode()))
                            mobileUser = sr.getProjectCode();
                        if ("A".equals(mobileUser)) {
                            userRType = 1;
                            ps.setSurveyUserUuid(surveyUser.getSurveyUserId());
                        }
                        if ("B".equals(mobileUser)) {
                            userRType = 2;
                            ps.setSurveyUserUuid(surveyUser.getSurveyUserId());
                        }
                        if ("C".equals(mobileUser)) {
                            userRType = 3;
                            ps.setSurveyUserUuid(sr.getUserId());
                        }
                        ps.setUserType(userRType);
                        PrpmImageSurvey prpmImageSurvey = mobileImageService.getImageSurveyByLossUuid(lossUuidList[i]);
                        if (prpmImageSurvey != null) {
                            ps.setCreateTime(new Date());
                            ps.setModifyBy(surveyUser != null ? surveyUser.getSurveyName() : sr.getUserId());
                            ps.setModifyTime(new Date());
                            mobileImageService.updateImageSurveyByLossUuid(ps);
                        } else {
                            ps.setIsDealing("1");// 未认领
                            ps.setCreateTime(new Date());
                            ps.setCreateBy(surveyUser != null ? surveyUser.getSurveyName() : sr.getUserId());
                            mobileImageService.saveSurveyImage(ps);
                        }
                    }
                    PrpmLossItemHistory prpmLossItemHistory = new PrpmLossItemHistory();
                    prpmLossItemHistory.setCaseInfoId(prpmLossItem.getCaseInfoId());
                    prpmLossItemHistory.setCaseNo(prpmLossItem.getCaseNo());
                    prpmLossItemHistory.setLossId(prpmLossItem.getLossId());
                    prpmLossItemHistory.setLossItemId(prpmLossItem.getLossItemId());
                    prpmLossItemHistory.setLossType(prpmLossItem.getLossType());
                    prpmLossItemHistory.setReassignPlat(Constant.REASSIGN_PLAT_APP);
                    prpmLossItemHistory.setOptType(OptConstant.MP_UPLOAD_SUCC);
                    prpmLossItemHistory.setOperateTime(new Date());
                    prpmLossItemHistory.setUserCode(sr.getUserId());
                    prpmLossItemHistory.setCreateBy(surveyUser != null ? surveyUser.getSurveyName() : sr.getUserId());
                    prpmLossItemHistory.setCreateTime(new Date());
                    lossItemHistoryList.add(prpmLossItemHistory);

                    lossUuidAList.add(lossUuidList[i]);
                }
            }
        } else {
            PrpmLossItem prpmLossItem = mobileLossService.getLossItemByLossUuid(sr.getLossUuid());
            if (prpmLossItem != null) {
                ps.setCaseNo(prpmLossItem.getCaseNo());
                ps.setLossId(prpmLossItem.getLossId());
                ps.setCaseInfoId(prpmLossItem.getCaseInfoId());
                ps.setLossItemId(prpmLossItem.getLossItemId());
                ps.setLossType(String.valueOf(prpmLossItem.getLossType()));
                ps.setImageSurveyId(UUIDUtils.getUUIDStr());
                ps.setSurveyUserCode(sr.getUserId());
                ps.setCreateTime(new Date());
                ps.setDealFlag("0");
                ps.setRemark(sr.getRemark());
//              ps.setUserCode(sr.getUserId());
//              ps.setUserName(surveyUser != null ? surveyUser.getSurveyName() : sr.getUserId());
                ps.setUserId(surveyUser != null ? surveyUser.getSurveyName() : sr.getUserId());
                String mobileUser = "";
                int userRType = 2;
                if (StringUtils.isNotBlank(sr.getProjectCode()))
                    mobileUser = sr.getProjectCode();
                if ("A".equals(mobileUser)) {
                    userRType = 1;
                    ps.setSurveyUserUuid(surveyUser.getSurveyUserId());
                }
                if ("B".equals(mobileUser)) {
                    userRType = 2;
                    ps.setSurveyUserUuid(surveyUser.getSurveyUserId());
                }
                if ("C".equals(mobileUser)) {
                    userRType = 3;
                    ps.setSurveyUserUuid(sr.getUserId());
                }
                ps.setUserType(userRType);
                PrpmImageSurvey prpmImageSurvey = mobileImageService.getImageSurveyByLossUuid(sr.getLossUuid());
                if (prpmImageSurvey != null) {
                    ps.setCreateTime(new Date());
                    ps.setModifyBy(surveyUser != null ? surveyUser.getSurveyName() : sr.getUserId());
                    ps.setModifyTime(new Date());
                    mobileImageService.updateImageSurveyByLossUuid(ps);
                } else {
                    ps.setIsDealing("1");// 未认领
                    ps.setCreateTime(new Date());
                    ps.setCreateBy(surveyUser != null ? surveyUser.getSurveyName() : sr.getUserId());
                    mobileImageService.saveSurveyImage(ps);
                }
                // 保存操作记录
                PrpmLossItemHistory prpmLossItemHistory = new PrpmLossItemHistory();
                prpmLossItemHistory.setCaseInfoId(prpmLossItem.getCaseInfoId());
                prpmLossItemHistory.setCaseNo(prpmLossItem.getCaseNo());
                prpmLossItemHistory.setLossId(prpmLossItem.getLossId());
                prpmLossItemHistory.setLossItemId(prpmLossItem.getLossItemId());
                prpmLossItemHistory.setLossType(prpmLossItem.getLossType());
                prpmLossItemHistory.setReassignPlat(Constant.REASSIGN_PLAT_APP);
                prpmLossItemHistory.setOptType(OptConstant.MP_UPLOAD_SUCC);
                prpmLossItemHistory.setOperateTime(new Date());
                prpmLossItemHistory.setUserCode(sr.getUserId());
                prpmLossItemHistory.setCreateBy(surveyUser != null ? surveyUser.getSurveyName() : sr.getUserId());
                prpmLossItemHistory.setCreateTime(new Date());
                lossItemHistoryList.add(prpmLossItemHistory);

                lossUuidAList.add(sr.getLossUuid());
            } else {
                // 错误
                response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_LOSSUUID_NO_ERROR,
                        MobileReturnCodeSupport.SYS_CODE, null, BaseResponse.class);
                return response;
            }
        }
        // 1调用CLD02接口
        TaskStatusRequest claimRequest = new TaskStatusRequest();
        claimRequest.setLogUuid(sr.getLogUuid());
        claimRequest.setCaseNo(sr.getCaseNo());
        claimRequest.setCaseUuid(sr.getCaseUuid());
        claimRequest.setLossUuidList(lossUuidAList);
        claimRequest.setTaskStatus("8");
        ResultInfo resultInfo = ClaimInterface.updateTaskStatus(claimRequest);
        if (resultInfo.getResultCode().equals("000")) {
            // 2更新任务状态
            PrpmLossItem prpmUpdateLossItem = new PrpmLossItem();
            prpmUpdateLossItem.setLossItemIdList(lossUuidAList);
            prpmUpdateLossItem.setLossStatus(8);
            prpmUpdateLossItem.setModifyBy(surveyUser != null ? surveyUser.getSurveyName() : sr.getUserId());
            prpmUpdateLossItem.setModifyTime(new Date());
            lossItemService.updateLossStatusByUuidList(prpmUpdateLossItem);
            // 3更新整案状态
            PrpmCaseInfo record = new PrpmCaseInfo();
            record.setCaseInfoId(sr.getCaseUuid());
            record.setModifyBy(surveyUser != null ? surveyUser.getSurveyName() : sr.getUserId());
            record.setModifyTime(new Date());
            record.setCaseStatus(Constant.CASE_STATUS_DEALING);
            caseInfoService.updateByPrimaryKeySelective(record);
            // 4记录状态变更历史表
            prpmLossItemHistoryService.saveList(lossItemHistoryList);
            response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_SUCCESS,
                    MobileReturnCodeSupport.SYS_CODE, null, BaseResponse.class);
        } else {
            // 错误
            response = MobileReturnCodeSupport.buildReponse2(MobileReturnCodeSupport.CODE_FAIL,
                    MobileReturnCodeSupport.SYS_CODE, resultInfo.getResultMsg(), BaseResponse.class);
            return response;
        }
        return response;
    }

    @Override
    public BaseResponse surveyFinish4C(SurveyFinishRequest sr) throws Exception {
        BaseResponse response = null;
        String userType = "";
        PrpmSurveyUser surveyUser = null;
        if (StringUtils.isNotBlank(sr.getProjectCode()))
            userType = sr.getProjectCode();
        if ("A".equals(userType)) {
            surveyUser = prpmSurveyUserService.findUserByUserCodeAndType(sr.getUserId(), "2");
        }
        if ("B".equals(userType)) {
            surveyUser = prpmSurveyUserService.findUserByUserCodeAndType(sr.getUserId(), "1");
        }
        List<PrpmLossItemHistory> lossItemHistoryList = new ArrayList<PrpmLossItemHistory>();
        List<String> lossUuidAList = new ArrayList<String>();
        // 如果有分号分隔的话，就是多个任务一起提交
        // 如果有多个任务，判断有无查勘任务，如果有查勘任务就过滤掉查勘任务，但是需要绑定到第一个损失项下面
        if (sr.getLossItemList().size() > 1) {
            List<LossItemDto> lossItemList = sr.getLossItemList();
            Collections.sort(lossItemList);
            if (StringUtils.endsWith(lossItemList.get(0).getLossId(), "0")) {
                lossUuidAList.add(lossItemList.get(0).getLossUuid());
                for (int i = 1; i < lossItemList.size(); i++) {
                    PrpmLossItem prpmLossItem = mobileLossService
                            .getLossItemByLossUuid(lossItemList.get(i).getLossUuid());
                    // 查询定损任务信息
                    PrpmImageSurvey ps = new PrpmImageSurvey();
                    if (prpmLossItem != null) {
                        ps.setCaseNo(prpmLossItem.getCaseNo());
                        ps.setLossId(prpmLossItem.getLossId());
                        ps.setCaseInfoId(prpmLossItem.getCaseInfoId());
                        ps.setLossItemId(prpmLossItem.getLossItemId());
                        ps.setLossType(String.valueOf(prpmLossItem.getLossType()));
                        ps.setImageSurveyId(UUIDUtils.getUUIDStr());
                        ps.setSurveyUserCode(sr.getUserId());
                        ps.setCreateTime(new Date());
                        ps.setDealFlag("0");
                        ps.setRemark(sr.getRemark());
//                      ps.setUserCode(sr.getUserId());
//                      ps.setUserName(surveyUser != null ? surveyUser.getSurveyName() : sr.getUserId());
                        ps.setUserId(surveyUser != null ? surveyUser.getSurveyName() : sr.getUserId());
                        String mobileUser = "";
                        int userRType = 2;
                        if (StringUtils.isNotBlank(sr.getProjectCode()))
                            mobileUser = sr.getProjectCode();
                        if ("A".equals(mobileUser)) {
                            userRType = 1;
                            ps.setSurveyUserUuid(surveyUser.getSurveyUserId());
                        }
                        if ("B".equals(mobileUser)) {
                            userRType = 2;
                            ps.setSurveyUserUuid(surveyUser.getSurveyUserId());
                        }
                        if ("C".equals(mobileUser)) {
                            userRType = 3;
                            ps.setSurveyUserUuid(sr.getUserId());
                        }
                        ps.setUserType(userRType);
                        PrpmImageSurvey prpmImageSurvey = mobileImageService
                                .getImageSurveyByLossUuid(lossItemList.get(i).getLossUuid());
                        if (prpmImageSurvey != null) {
                            mobileImageService.updateImageSurveyByLossUuid(ps);
                        } else {
                            if (i == 1) {
                                ps.setRelationId(lossItemList.get(0).getLossUuid());
                            }
                            ps.setIsDealing("1");// 未认领
                            mobileImageService.saveSurveyImage(ps);
                        }
                        response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_SUCCESS,
                                MobileReturnCodeSupport.SYS_CODE, null, BaseResponse.class);
                    }
                    PrpmLossItemHistory prpmLossItemHistory = new PrpmLossItemHistory();
                    prpmLossItemHistory.setCaseInfoId(prpmLossItem.getCaseInfoId());
                    prpmLossItemHistory.setCaseNo(prpmLossItem.getCaseNo());
                    prpmLossItemHistory.setLossId(prpmLossItem.getLossId());
                    prpmLossItemHistory.setLossItemId(prpmLossItem.getLossItemId());
                    prpmLossItemHistory.setLossType(prpmLossItem.getLossType());
                    prpmLossItemHistory.setReassignPlat(Constant.REASSIGN_PLAT_APP);
                    prpmLossItemHistory.setOptType(OptConstant.MP_UPLOAD_SUCC);
                    prpmLossItemHistory.setOperateTime(new Date());
                    prpmLossItemHistory.setUserCode(sr.getUserId());
                    prpmLossItemHistory.setCreateBy(surveyUser != null ? surveyUser.getSurveyName() : sr.getUserId());
                    prpmLossItemHistory.setCreateTime(new Date());
                    lossItemHistoryList.add(prpmLossItemHistory);

                    lossUuidAList.add(lossItemList.get(i).getLossUuid());
                }
            } else {
                for (int i = 0; i < lossItemList.size(); i++) {
                    PrpmLossItem prpmLossItem = mobileLossService
                            .getLossItemByLossUuid(lossItemList.get(i).getLossUuid());
                    // 查询定损任务信息
                    PrpmImageSurvey ps = new PrpmImageSurvey();
                    if (prpmLossItem != null) {
                        ps.setCaseNo(prpmLossItem.getCaseNo());
                        ps.setLossId(sr.getLossUuid());
                        ps.setSurveyUserUuid(sr.getUserId());
                        ps.setImageSurveyId(UUIDUtils.getUUIDStr());
                        ps.setSurveyUserCode(sr.getUserId());
                        ps.setLossId(sr.getLossId());
                        ps.setCreateTime(new Date());
                        ps.setDealFlag("0");
                        ps.setRemark(sr.getRemark());
//                      ps.setUserCode(sr.getUserId());
//                      ps.setUserName(surveyUser != null ? surveyUser.getSurveyName() : sr.getUserId());
                        ps.setUserId(surveyUser != null ? surveyUser.getSurveyName() : sr.getUserId());
                        String mobileUser = "";
                        int userRType = 2;
                        if (StringUtils.isNotBlank(sr.getProjectCode()))
                            mobileUser = sr.getProjectCode();
                        if ("A".equals(mobileUser)) {
                            userRType = 1;
                            ps.setSurveyUserUuid(surveyUser.getSurveyUserId());
                        }
                        if ("B".equals(mobileUser)) {
                            userRType = 2;
                            ps.setSurveyUserUuid(surveyUser.getSurveyUserId());
                        }
                        if ("C".equals(mobileUser)) {
                            userRType = 3;
                            ps.setSurveyUserUuid(sr.getUserId());
                        }
                        ps.setUserType(userRType);
                        PrpmImageSurvey prpmImageSurvey = mobileImageService
                                .getImageSurveyByLossUuid(lossItemList.get(i).getLossUuid());
                        if (prpmImageSurvey != null) {
                            mobileImageService.updateImageSurveyByLossUuid(ps);
                        } else {
                            mobileImageService.saveSurveyImage(ps);
                        }
                        PrpmLossItemHistory prpmLossItemHistory = new PrpmLossItemHistory();
                        prpmLossItemHistory.setCaseInfoId(prpmLossItem.getCaseInfoId());
                        prpmLossItemHistory.setCaseNo(prpmLossItem.getCaseNo());
                        prpmLossItemHistory.setLossId(prpmLossItem.getLossId());
                        prpmLossItemHistory.setLossItemId(prpmLossItem.getLossItemId());
                        prpmLossItemHistory.setLossType(prpmLossItem.getLossType());
                        prpmLossItemHistory.setReassignPlat(Constant.REASSIGN_PLAT_APP);
                        prpmLossItemHistory.setOptType(OptConstant.MP_UPLOAD_SUCC);
                        prpmLossItemHistory.setOperateTime(new Date());
                        prpmLossItemHistory.setUserCode(sr.getUserId());
                        prpmLossItemHistory
                                .setCreateBy(surveyUser != null ? surveyUser.getSurveyName() : sr.getUserId());
                        prpmLossItemHistory.setCreateTime(new Date());
                        lossItemHistoryList.add(prpmLossItemHistory);
                        lossUuidAList.add(lossItemList.get(i).getLossUuid());
                    }
                }
            }
        } else {
            PrpmLossItem prpmLossItem = mobileLossService
                    .getLossItemByLossUuid(sr.getLossItemList().get(0).getLossUuid());
            // 查询定损任务信息
            PrpmImageSurvey ps = new PrpmImageSurvey();
            if (prpmLossItem != null) {
                ps.setCaseNo(prpmLossItem.getCaseNo());
                ps.setLossId(prpmLossItem.getLossId());
                ps.setCaseInfoId(prpmLossItem.getCaseInfoId());
                ps.setLossItemId(prpmLossItem.getLossItemId());
                ps.setLossType(String.valueOf(prpmLossItem.getLossType()));
                ps.setImageSurveyId(UUIDUtils.getUUIDStr());
                ps.setSurveyUserCode(sr.getUserId());
                ps.setCreateTime(new Date());
                ps.setDealFlag("0");
                ps.setRemark(sr.getRemark());
//              ps.setUserCode(sr.getUserId());
//              ps.setUserName(surveyUser != null ? surveyUser.getSurveyName() : sr.getUserId());
                ps.setUserId(surveyUser != null ? surveyUser.getSurveyName() : sr.getUserId());
                String mobileUser = "";
                int userRType = 2;
                if (StringUtils.isNotBlank(sr.getProjectCode()))
                    mobileUser = sr.getProjectCode();
                if ("A".equals(mobileUser)) {
                    userRType = 1;
                    ps.setSurveyUserUuid(surveyUser.getSurveyUserId());
                }
                if ("B".equals(mobileUser)) {
                    userRType = 2;
                    ps.setSurveyUserUuid(surveyUser.getSurveyUserId());
                }
                if ("C".equals(mobileUser)) {
                    userRType = 3;
                    ps.setSurveyUserUuid(sr.getUserId());
                }
                ps.setUserType(userRType);

                PrpmLossItemHistory prpmLossItemHistory = new PrpmLossItemHistory();
                prpmLossItemHistory.setCaseInfoId(prpmLossItem.getCaseInfoId());
                prpmLossItemHistory.setCaseNo(prpmLossItem.getCaseNo());
                prpmLossItemHistory.setLossId(prpmLossItem.getLossId());
                prpmLossItemHistory.setLossItemId(prpmLossItem.getLossItemId());
                prpmLossItemHistory.setLossType(prpmLossItem.getLossType());
                prpmLossItemHistory.setReassignPlat(Constant.REASSIGN_PLAT_APP);
                prpmLossItemHistory.setOptType(OptConstant.MP_UPLOAD_SUCC);
                prpmLossItemHistory.setOperateTime(new Date());
                prpmLossItemHistory.setUserCode(sr.getUserId());
                prpmLossItemHistory.setCreateBy(surveyUser != null ? surveyUser.getSurveyName() : sr.getUserId());
                prpmLossItemHistory.setCreateTime(new Date());
                lossItemHistoryList.add(prpmLossItemHistory);
                lossUuidAList.add(sr.getLossItemList().get(0).getLossUuid());
                PrpmImageSurvey prpmImageSurvey = mobileImageService
                        .getImageSurveyByLossUuid(sr.getLossItemList().get(0).getLossUuid());
                if (prpmImageSurvey != null) {
                    mobileImageService.updateImageSurveyByLossUuid(ps);
                } else {
                    ps.setIsDealing("1");// 未认领
                    mobileImageService.saveSurveyImage(ps);
                }
            } else {
                // 错误
                response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_LOSSUUID_NO_ERROR,
                        MobileReturnCodeSupport.SYS_CODE, null, BaseResponse.class);
                return response;
            }
        }
        // 1调用CLD02接口
        TaskStatusRequest claimRequest = new TaskStatusRequest();
        claimRequest.setLogUuid(sr.getLogUuid());
        claimRequest.setCaseNo(sr.getCaseNo());
        claimRequest.setCaseUuid(sr.getCaseUuid());
        claimRequest.setLossUuidList(lossUuidAList);
        claimRequest.setTaskStatus("8");
        ResultInfo resultInfo = ClaimInterface.updateTaskStatus(claimRequest);
        if (resultInfo.getResultCode().equals("000")) {
            // 2更新任务状态
            PrpmLossItem prpmQueryLossItem = new PrpmLossItem();
            prpmQueryLossItem.setLossItemIdList(lossUuidAList);
            List<PrpmLossItem> prpmLossItemList = lossItemService.selectLossItemByLossUuids(prpmQueryLossItem);
            for(PrpmLossItem item : prpmLossItemList){
                String lossDealType = item.getLossDealType();
                if("0".equals(lossDealType)){
                    lossDealType = "2";
                }else if("1".equals(lossDealType)){
                    lossDealType = "3";
                }
                PrpmLossItem prpmUpdateLossItem = new PrpmLossItem();
                prpmUpdateLossItem.setLossItemId(item.getLossItemId());
                prpmUpdateLossItem.setLossStatus(8);
                prpmUpdateLossItem.setModifyBy(surveyUser != null ? surveyUser.getSurveyName() : sr.getUserId());
                prpmUpdateLossItem.setModifyTime(new Date());
                prpmUpdateLossItem.setLossDealType(lossDealType);
                lossItemService.updateLossItem(prpmUpdateLossItem);
            }
            // 3更新整案状态
            PrpmCaseInfo record = caseInfoService.selectByPrimaryKey(sr.getCaseUuid());
            if(record != null){
                record.setCaseInfoId(sr.getCaseUuid());
                record.setModifyBy(surveyUser != null ? surveyUser.getSurveyName() : sr.getUserId());
                record.setModifyTime(new Date());
                record.setCaseStatus(Constant.CASE_STATUS_DEALING);
                String caseDealType = record.getCaseDealType();
                if("0".equals(caseDealType)){
                    caseDealType = "2";
                }else if("1".equals(caseDealType)){
                    caseDealType = "3";
                }
                record.setCaseDealType(caseDealType);
                caseInfoService.updateByPrimaryKeySelective(record);
            }
            
            // 4记录状态变更历史表
            prpmLossItemHistoryService.saveList(lossItemHistoryList);
            response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_SUCCESS,
                    MobileReturnCodeSupport.SYS_CODE, null, BaseResponse.class);
        } else {
            // 错误
            response = MobileReturnCodeSupport.buildReponse2(MobileReturnCodeSupport.CODE_FAIL,
                    MobileReturnCodeSupport.SYS_CODE, resultInfo.getResultMsg(), BaseResponse.class);
            return response;
        }
        return response;
    }

    @Override
    public LossItemListResponse getCaseList(LossItemListRequest request) throws Exception {
        String userCode = request.getUserId();// 用户编码
        String lastTwo = request.getLastTwo(); // 案件号后两位
        String caseStartTime = request.getCaseStartTime(); // 案件下发开始时间
        String caseEndTime = request.getCaseEndTime(); // 案件下发结束时间
        LossItemListResponse response = new LossItemListResponse();
        String source = request.getProjectCode();
        if (StringUtils.isBlank(userCode)) {
            LogUtils.info("MP61获取定损单案件列表->logUuid:" + request.getLogUuid() + "userId为空");
            response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.KEY_MOBILE_00043,
                    MobileReturnCodeSupport.SYS_CODE, null, LossItemListResponse.class);
        } else {
            // 获取案件下任务列表
            List<PrpmLossItem> items = mobileLossService.getLossListByUserCodeOrLastTwo(userCode, lastTwo, source);
            // 已去重caseUuid的集合
            List<String> caseUUidList = new ArrayList<String>();
            Map<String, Integer> lossStatusMap = new HashMap<String, Integer>();
            if (items != null && items.size() > 0) {
                for (PrpmLossItem item : items) {
                    String caseInfoId = item.getCaseInfoId();
                    // 返回指定集合中指定对象出现的次数
                    if (Collections.frequency(caseUUidList, caseInfoId) < 1){
                        caseUUidList.add(caseInfoId);
                    }
                }
                List<PrpmLossItem> prpmLossStatusList = new ArrayList<PrpmLossItem>();
                for (int i = 0; i < caseUUidList.size(); i++) {
                    for (int j = 0; j < items.size(); j++) {
                        if (StringUtils.endsWith(items.get(j).getCaseInfoId(), caseUUidList.get(i))) {
                            prpmLossStatusList.add(items.get(j));
                        }
                    }
                    // 确认次数
                    int flag = 0;
                    innerLoop: for (int j = 0; j < prpmLossStatusList.size(); j++) {
                        int lossItemStatus = prpmLossStatusList.get(j).getLossitemStatus();
                        if (lossItemStatus == 2 || lossItemStatus == 3 || lossItemStatus == 4 || lossItemStatus == 5) {
                            flag = flag + 1;
                            break innerLoop;
                        }
                    }
                    lossStatusMap.put(caseUUidList.get(i), flag);
                }
                if (caseUUidList != null && caseUUidList.size() > 0) {
                    Map<String, Object> params = new HashMap<String, Object>();
                    params.put("list", caseUUidList);
                    params.put("caseStartTime", caseStartTime);
                    params.put("caseEndTime", caseEndTime);
                    List<PrpmCaseInfo> caseInfoList = caseInfoService.getCaseInfoByUuidOrTime(params);// 获取caseUUid获取prpmcaseinfo
                    if (caseInfoList != null && !"".equals(caseInfoList)) {
                        List<CaseInfoDto> caselist = new ArrayList<CaseInfoDto>();
                        for (PrpmCaseInfo prpmCaseInfo : caseInfoList) {
                            CaseInfoDto caseInfodto = new CaseInfoDto();
                            caseInfodto.setCaseNo(prpmCaseInfo.getCaseNo());
                            String caseInfoId = prpmCaseInfo.getCaseInfoId();
                            caseInfodto.setCaseUuid(caseInfoId);
                            String singleLossStatus = "";
                            if(lossStatusMap.get(caseInfoId) == 0){
                                singleLossStatus = "1"; // 未确认
                            }else if(lossStatusMap.get(caseInfoId) == prpmLossStatusList.size()){
                                singleLossStatus = "3"; // 全部确认
                            }else{
                                singleLossStatus = "2"; // 部分确认
                            }
                            caseInfodto.setSingleLossStatus(singleLossStatus);
                            caselist.add(caseInfodto);
                        }
                        /* -封装损失信息对象end - */
                        response.setCaseInfoList(caselist);
                        response.setResultCode(MobileReturnCodeSupport.CODE_SUCCESS);
                        response.setSystemCode(MobileReturnCodeSupport.SYS_CODE);
                    }
                }
            } else {
                LogUtils.info("MP61--获取定损单案件列表--根据用户编码和案件号后两位查询不到数据！");
                response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_SUCCESS,
                        MobileReturnCodeSupport.SYS_CODE, null, LossItemListResponse.class);
            }
        }
        return response;
    }

}
